<?php

namespace ABonus\Bundle\ExtractBundle\Manager;

use Doctrine\ORM\EntityManager;
use ABonus\Bundle\ExtractBundle\Entity;
use ABonus\Bundle\ExtractBundle\Repository;

/**
 * Description of BlocManager
 *
 */
class ImageManager
{
    protected $repository;
    protected $em;
    protected $kernel;

    public function __construct(EntityManager $em, $entityClass, $kernel)
    {
        $this->em = $em;
        $this->repository = $em->getRepository($entityClass);
        $this->kernel = $kernel;
    }

    public function getRepository(){
        return $this->repository;
    }

    public function saveImage($newImage){
        $oldImage = $this->repository->findOneBy(array('name' => $newImage->getName()));
        if($oldImage == null){
            $this->em->persist($newImage);
            $this->em->flush();
            return $newImage;
        }else{
            $oldImage->setPath($newImage->getPath());
            $oldImage->setName($newImage->getName());
            $oldImage->upload();
            $this->em->persist($oldImage);
            $this->em->flush();
            return $oldImage;
        }
    }
}
